package com.ruoyi.legal.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.legal.mapper.LetterMapper;
import com.ruoyi.legal.domain.Letter;
import com.ruoyi.legal.service.ILetterService;

/**
 * 介绍信Service业务层处理
 * 
 * @author xwj
 * @date 2025-05-08
 */
@Service
public class LetterServiceImpl extends ServiceImpl<LetterMapper, Letter> implements ILetterService {
    @Autowired
    private LetterMapper letterMapper;

    /**
     * 查询介绍信
     * 
     * @param id 介绍信主键
     * @return 介绍信
     */
    @Override
    public Letter selectLetterById(Long id) {
        return letterMapper.selectLetterById(id);
    }

    /**
     * 查询介绍信列表
     * 
     * @param letter 介绍信
     * @return 介绍信
     */
    @Override
    public List<Letter> selectLetterList(Letter letter) {
        return letterMapper.selectLetterList(letter);
    }

    /**
     * 新增介绍信
     * 
     * @param letter 介绍信
     * @return 结果
     */
    @Override
    public Letter insertLetter(Letter letter) {

      //  letterMapper.insertLetter(letter);
    save(letter);
    return letter;
    }

    /**
     * 修改介绍信
     * 
     * @param letter 介绍信
     * @return 结果
     */
    @Override
    public Letter updateLetter(Letter letter) {

      //  letterMapper.updateLetter(letter);
        updateById(letter);
        return letter;
    }

    /**
     * 批量删除介绍信
     * 
     * @param ids 需要删除的介绍信主键
     * @return 结果
     */
    @Override
    public int deleteLetterByIds(Long[] ids) {
        return letterMapper.deleteLetterByIds(ids);
    }

    /**
    * 条件删除
    * @param letter 介绍信
    * @return
    */
    @Override
    public int delete(Letter letter) {
        QueryWrapper<Letter> qw = new QueryWrapper<>();
        QueryTool.condition(qw, letter);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param letter 介绍信
     * @return
     */
    @Override
    public Pages<Letter> queryPage(Letter letter) {
        QueryWrapper<Letter> qw = new QueryWrapper<>();
        QueryTool.condition(qw, letter);
        IPage<Letter> ipage = new Query<Letter>().getPage(letter);

        IPage<Letter> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    public String getNextSerialNo(String year) {
        Date now = new Date();
//        int year = DateUtil.year(now);

        if(StringUtils.isEmpty(year)) {
            int _year = DateUtil.year(now);
            year = "" + _year;
        }

        String max = letterMapper.selectMaxSerialNo(year);

        if(StringUtils.isEmpty(max)) {
            max = "0";
        }

        int _max = Convert.toInt(max, 0);
        _max = _max + 1;
        String origin = "" + _max;
        return StrUtil.padPre(origin, 3, "0");
    }

    /**
     * 删除介绍信信息
     * 
     * @param id 介绍信主键
     * @return 结果
     */
    @Override
    public int deleteLetterById(Long id) {
        return letterMapper.deleteLetterById(id);
    }
}
